package co.com.javeriana.aes.patrones.universalbank.service;

import co.com.javeriana.aes.patrones.universalbank.dto.CRMUser;
import co.com.javeriana.aes.patrones.universalbank.util.ConnectDB;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ejb.Stateless;

/**
 *
 * @author jonathan
 */
@WebService(serviceName = "CRM")
@Stateless()
public class CRM_Web_Service {

    /**
     * This is a sample web service operation
     * @param user_id
     * @return 
     */
    @WebMethod(operationName = "CRM_USER")
    public CRMUser getUserCRMDetails(@WebParam(name = "user_id") String user_id) {

        Statement st = null;
        ResultSet rs = null;
        CRMUser user = null;

        try {

            ConnectDB s = ConnectDB.getInstance();

            st = s.getConnection().createStatement();
            rs = st.executeQuery("select * from client where user_id=" + '"' + user_id + '"');

            user = new CRMUser();

            while (rs.next()) {
                user.setUserId(rs.getInt(1));
                user.setName(rs.getString(2));
                user.setLastName(rs.getString(3));
                user.setProfileNumber(rs.getString(4));
                user.setAddress(rs.getString(5));
                user.setEmail(rs.getString(6));
                user.setContactNumber(rs.getString(7));
            }

        } catch (ClassNotFoundException | SQLException e) {
            Logger.getLogger(CRM_Web_Service.class.getName()).log(Level.SEVERE, null, e);
        } finally {

            try {
                st.close();
                rs.close();
            } catch (SQLException ex) {
                Logger.getLogger(CRM_Web_Service.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        return user;
    }
}
